using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Weathertastic
{
    class WeatherMenuItem : MenuItem
    {
        private Image _ItemImage;
        private string ItemName = string.Empty;
        private Font Font;

        public WeatherMenuItem(string itemName, Image itemImage)
            : base()
        {
            this.OwnerDraw = true;
            this.Text = itemName;
            this.Font = SystemFonts.MenuFont;
            this._ItemImage = itemImage;

        }

        public WeatherMenuItem(string itemName)
            : base()
        {
            this.OwnerDraw = true;
            this.Text = itemName;
            this.Font = SystemFonts.MenuFont;
        }

        protected override void OnMeasureItem(MeasureItemEventArgs e)
        {
            base.OnMeasureItem(e);

            if (this.Text.Equals("-"))
            {
                // Separator shouldn't be quite as big as the text
                e.ItemWidth = (int)e.Graphics.MeasureString(this.Text, this.Font).Width + 10;
                e.ItemHeight = (int)e.Graphics.MeasureString(this.Text, this.Font).Height - 5; ;
            }

            else
            {
                e.ItemHeight = (int)e.Graphics.MeasureString(this.Text, this.Font).Height + 5;
                //e.ItemHeight = 25;
                e.ItemWidth = (int)e.Graphics.MeasureString(this.Text, this.Font).Width + 30;
            }

        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            base.OnDrawItem(e);

            if (this.Text.Equals("-"))
            {
                Pen pen = new Pen(SystemColors.ControlDark);
                int xLeft = e.Bounds.Left;
                int xRight = (xLeft + e.Bounds.Width);
                int yCenter = e.Bounds.Top + (e.Bounds.Height / 2);
                e.Graphics.DrawLine(pen, xLeft, yCenter, xRight, yCenter);

                pen = new Pen(SystemColors.ControlLightLight);
                e.Graphics.DrawLine(pen, xLeft + 1, yCenter + 1, xRight, yCenter + 1);
            }

            else
            {
                Color textColor;
                if (!this.Enabled)
                {
                    textColor = SystemColors.GrayText;
                }

                else
                {
                    if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
                    {
                        e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds);
                        textColor = SystemColors.HighlightText;
                    }
                    else
                    {
                        e.Graphics.FillRectangle(SystemBrushes.Menu, e.Bounds);
                        textColor = SystemColors.MenuText;
                    }
                }

                // Draw the image.
                if (ItemImage != null)
                {
                    if (this.Enabled == false)
                    {
                        ControlPaint.DrawImageDisabled(e.Graphics, ItemImage,
                        e.Bounds.Left + 3, e.Bounds.Top + 2, SystemColors.Menu);
                    }
                    else
                    {
                        e.Graphics.DrawImage(ItemImage, e.Bounds.Left + 3, e.Bounds.Top + 2, 16, 16);
                    }
                }

                // Draw the text with the supplied colors and in the set region.
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(textColor), e.Bounds.Left + 25, e.Bounds.Top + 3);
            }

        }

        public Image ItemImage
        {
            get { return this._ItemImage; }
            set { this._ItemImage = value; }
        }
    }
}
